package imse.team0.processors;

import imse.team0.customers.ClientInfoStorage;
import imse.team0.db.dal.LocalInventoryQueryService;
import imse.team0.orders.Order;
import imse.team0.transportation.TransportationOrder;
import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 5/1/12
 * Time: 11:02 AM
 * To change this template use File | Settings | File Templates.
 */

public class TransportationProcessing {

    private static Logger LOG = LoggerFactory.getLogger(TemporaryOrderStore.class.getName());
    private LocalInventoryQueryService queryService = new LocalInventoryQueryService();

    public TransportationOrder createTransportationOrder(Exchange exchange) throws SQLException {
        final String localInventoryAddressID = "2";
        TransportationOrder newTransportationOrder = new TransportationOrder();
        int totalLoadVolume = 0;
        Order order = exchange.getIn().getBody(Order.class);
        for (Order.OrderItem item : order.items) {
            int itemVolume = queryService.retrieveProductVolume(item.itemId);
            totalLoadVolume = totalLoadVolume + item.quantity * itemVolume;
        }
        String clientAddress = ClientInfoStorage.getClientAddress(order.clientID);
        if ((clientAddress != null) && (totalLoadVolume != 0)) {
            newTransportationOrder.shipmentRequestID = order.orderid;
            newTransportationOrder.startAddress = localInventoryAddressID;
            newTransportationOrder.endAddress = clientAddress;
            newTransportationOrder.loadVolume = totalLoadVolume;
        }
        exchange.getIn().setHeader("TransportationOrderID", newTransportationOrder.shipmentRequestID);
        return newTransportationOrder;
    }
}